package end.ser.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import end.bean.shiro.Resource;
import end.bean.shiro.Role;
import end.dao.ResourceDAO;
import end.dao.RoleDAO;
import end.ser.RoleSER;

/**
 * 
 * ManagerSER.java 2016-6-25
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Service("roleSER")
public class RoleSERImp<T> extends BaseSERImp<T> implements RoleSER<T> {
	@Autowired
	private RoleDAO<T> mapperDAO;

	public RoleDAO<T> getMapperDAO() {
		return mapperDAO;
	}

	public void setMapperDAO(RoleDAO<T> mapperDAO) {
		this.mapperDAO = mapperDAO;
	}

	@Autowired
	private ResourceDAO<Resource> resourceDAO;

	public ResourceDAO<Resource> getResourceDAO() {
		return resourceDAO;
	}

	public void setResourceDAO(ResourceDAO<Resource> resourceDAO) {
		this.resourceDAO = resourceDAO;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryRolesByManagerId(Integer id) {
		List<Role> roles = (List<Role>) getMapperDAO()
				.queryRolesByManagerId(id);
		for (Role role : roles) {
			role.setResources(resourceDAO.queryFunctionByRoleId(role.getId()));
		}
		return (List<T>) roles;
	}

	@Override
	public Integer[] queryRoleRightsByManager(Integer id) {
		return getMapperDAO().queryRoleRightsByManager(id);
	}

	@Override
	public boolean saveRoleAuthByManager(Integer[] roleID, Integer managerID) {
		return getMapperDAO().saveRoleAuthByManager(roleID, managerID);
	}

	@Override
	public boolean isRoleCodeAlreadyUsed(Role role) {
		if (mapperDAO.isRoleCodeAlreadyUsed(role) > 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean modifyRole(Role role) {
		return mapperDAO.modifyRole(role);
	}
}
